package com.kinghunt.misp.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.regex.Pattern;

/**
 * 时间工具类
 */
@Slf4j
public class DateUtil {
    /**
     * 将一个string类型的时间转换为标准时间，会处理2020，2020-1，2020-01等不是标准yyyy-MM-dd HH:mm:ss格式的时间，会给一个当年首天、当月首天的标准时间格式
     *
     * @param date
     * @return
     */
    public static Date formatStringToDate(String date) {
        if (StringUtils.isBlank(date) || StringUtils.isAlpha(date)) {
            log.warn("DateUtil.formatStringToDate入参非法, result: {}", date);
            return null;
        }
        Date result = null;
        try {
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern(
                    "[yyyy-MM-dd][yyyy-M-dd][yyyy-MM-d][yyyy-M-d][yyyy-MM-dd HH:mm:ss][yyyy-M-dd HH:mm:ss][yyyy-M][yyyy-MM]");
            final String newDate = date.replaceAll("\\.", "-").replaceAll("\\[", "").replaceAll("]", "").replaceAll("/", "-");
            log.info("newDate=" + newDate);
            Instant instant = LocalDate.parse(fillMonthAndDay(newDate), dtf)
                    .atStartOfDay()
                    .atZone(ZoneId.of("Asia/Shanghai"))
                    .toInstant();
            result = Date.from(instant);
        } catch (Exception e) {
            log.error("DateUtil.formatStringToDate 异常,忽略结果, date: " + date, e);
        }
        log.info("DateUtil.formatStringToDate处理完成, result: {}", result);
        return result;
    }

    private static String fillMonthAndDay(String date) {
        String yearAndMonthRegex1 = "\\d{4}-\\d{2}";
        String yearAndMonthRegex2 = "\\d{4}-\\d{1}";
        String yearAndMonthRegex3 = "\\d{4}-\\d{2}-";
        String yearRegex = "\\d{4}";
        String yearRegex2 = "\\d{4}-";
        String yearRegex3 = "[a-zA-Z]\\d{4}";
        Pattern yearAndMonthPattern1 = Pattern.compile(yearAndMonthRegex1);
        Pattern yearAndMonthPattern2 = Pattern.compile(yearAndMonthRegex2);
        Pattern yearAndMonthPattern3 = Pattern.compile(yearAndMonthRegex3);
        Pattern yearPattern = Pattern.compile(yearRegex);
        Pattern yearPatter2 = Pattern.compile(yearRegex2);
        Pattern yearPatter3 = Pattern.compile(yearRegex3);

        if (yearAndMonthPattern1.matcher(date).matches()) {
            return date + "-01";
        } else if (yearAndMonthPattern2.matcher(date).matches()) {
            return date + "-01";
        } else if (yearAndMonthPattern3.matcher(date).matches()) {
            return date + "01";
        } else if (yearPattern.matcher(date).matches()) {
            return date + "-01-01";
        } else if (yearPatter2.matcher(date).matches()) {
            return date + "01-01";
        } else if (yearPatter3.matcher(date).matches()) {
            return date.substring(1) + "-01-01";
        } else {
            return date;
        }
    }
}
